package com.bindapp.model;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import com.bindapp.R;
import com.bindapp.activity.YJListActivity;
import com.bindapp.adapter.ViewPagerAdapter;
import com.bindapp.databinding.FragmentMaintab1Binding;
import com.bindapp.fragment.Tab1One;
import com.bindapp.fragment.Tab1Three;
import com.bindapp.fragment.Tab1Two;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Tab1Model extends BaseModel{
    private FragmentMaintab1Binding binding;
    private ViewPagerAdapter adapter;

    private List<Fragment> fragmentList = new ArrayList<Fragment>();

    public Tab1Model(final Context context, FragmentManager manager) {
        super(context);
        binding=DataBindingUtil.inflate(LayoutInflater.from(context),R.layout.fragment_maintab1,null,false);
        binding.setClick(new ViewClick());
        binding.toolbar.setNavigationIcon(R.drawable.monitor);
        binding.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //Static.showToast(context,"点击");
            }
        });

        fragmentList.add(new Tab1One());
        fragmentList.add(new Tab1Two());
        fragmentList.add(new Tab1Three());
        adapter=new ViewPagerAdapter(manager,fragmentList);
        binding.viewpager.setAdapter(adapter);
        binding.viewpager.setOffscreenPageLimit(3);
        binding.viewpager.addOnPageChangeListener(new PageListener());

    }

    public class ViewClick{
        public void onClick(View view){
            switch(view.getId()){
                case R.id.bjbtn:
                    turnToVc(YJListActivity.class,new HashMap<String,Object>());
                    /*Intent intent=new Intent(context, BJActivity.class);
                    context.startActivity(intent);*/
                    break;
                case R.id.btn1:
                    binding.viewpager.setCurrentItem(0);
                    break;
                case R.id.btn2:
                    binding.viewpager.setCurrentItem(1);
                    break;
                case R.id.btn3:
                    binding.viewpager.setCurrentItem(2);
                    break;
            }

        }
    }

    class PageListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            selectPage(position,binding.btn1,binding.btn2,binding.btn3);

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    public void selectPage(int page,Button... views){
        for(Button view:views){
            view.setBackground(null);
            view.setTextColor(ContextCompat.getColor(context,R.color.grey_color4));
        }
        views[page].setBackground(ContextCompat.getDrawable(context,R.drawable.maintab1btnpass));
        views[page].setTextColor(Color.WHITE);

    }

    @Override
    public FragmentMaintab1Binding getBinding(){
        return binding;
    }

}
